import QuestionList from "./QuestionList";
import Question from "./Question";
import {__CONFIG__, apiUrl} from "../config/global"
export default class ApiQuestionList extends QuestionList{
  questionId:string;
  async initData(pageId) {
    this.questionId=pageId
    await fetch(apiUrl+"/api/question/questionList?id="+pageId)
      .then(res=>res.json())
      .then(res=>{
        if(res.code==1){
          this.name=res.data.name
          res.data.list.forEach(e=>{
            let item=new Question(e.title,e.answer,this.type)
            item.id=e.id
            item.bakAnswer=[...e.bakAnswer]
            this.list.push(item)
          })
        }
      })

  }
  createErrorList(oneOfFour) {
    let question=oneOfFour.question
    let answer=question.answer
    //随机一个方向 0:up 1:right 2:down 3:left
    let directionIndex=Math.floor(Math.random()*4);
    oneOfFour.rightDirection=directionIndex;
    //围绕正确答案，随机4个错误答案，去重
    let bakAnswer=[...question.bakAnswer]
    let errorAnswerList=bakAnswer.sort(()=>{
      return Math.random()-0.5
    })
    oneOfFour.answerUp=''+errorAnswerList[0];
    oneOfFour.answerRight=''+errorAnswerList[1];
    oneOfFour.answerDown=''+errorAnswerList[2];
    oneOfFour.answerLeft=''+errorAnswerList[3];
    if (directionIndex==0){
      oneOfFour.answerUp=''+answer
    }else if(directionIndex==1){
      oneOfFour.answerRight=''+answer
    }else if(directionIndex==2){
      oneOfFour.answerDown=''+answer
    }else{
      oneOfFour.answerLeft=''+answer
    }
  }
  async getProficiency(pageId){
    //未登录则不进行记录
    console.log('getProficiency',pageId)
    if (!__CONFIG__.userInfoObj||!__CONFIG__.userInfoObj.userId){
      console.log('getProficiency1',pageId)
      return "0.0"
    }
    if (this.oneOfFourList.length>0&&this.oneOfFourList[0].selectFlag!=0){
      let reportData=this.oneOfFourList.map(e=>{
        return {
        id:e.question.id,
          selectFlag:e.selectFlag
      }
    })
      console.log('reportData',reportData)
      let report=await fetch(apiUrl+'/api/question/report?id='+pageId+'&userId='+__CONFIG__.userInfoObj.userId,
        {
          method:'POST',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
          },
          body:JSON.stringify({
            data:reportData
          })
        })
        .then(res=>res.json())
      console.log('report',report)
      return report.data.proficiency
    }else{
      console.log('getProficiency',apiUrl+"/api/question/proficiency?id="+pageId+"&userId="+__CONFIG__.userInfoObj.userId)
      let result=await fetch(apiUrl+"/api/question/proficiency?id="+pageId+"&userId="+__CONFIG__.userInfoObj.userId)
        .then(res=>res.json())
      console.log('result',result)
      return result.data.proficiency
    }

  }
}
